#coding=utf-8
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from PublicTool import HttpWrite


__author__ = 'czq'
#class Test:
#    def __init__(self):
#        pass
#    def get(self):
#        pass
#    def test(self):
#        print self.get() or 'aaa'
#
#class Test1(Test):
#    def __init__(self):
#        pass
#    def get(self):
#        return "a"
#
#Test1().test()

#Test().test()

class ViewHelper(object):
    def __init__(self):
        self.moduleName=None
        self.typeName=None
    def get_model(self):
        pass
    def getHtmlAddress(self,type):
        return "%s%s/%s_%s.html"%('',self.moduleName,self.typeName,type)

    #列表
    def list_get_list(self,request):
        pass
    def list_get_dict(self,request):
        pass
    def list(self,request):


        list = self.list_get_list(request)# or
        if list==None:
            list=self.get_model().objects.all()
        dict=self.list_get_dict(request) or {}
        #raise Exception,len(list)

        dict['list']=list
        return render_to_response(self.getHtmlAddress("list"), dict,context_instance=RequestContext(request))

    def show_response(self,request,instance,dict,url='show'):
        return render_to_response(self.getHtmlAddress(url), dict,context_instance=RequestContext(request))
    #显示
    def show_submit(self,request,instance):
        pass
    def show_fun(self,request,instance):
        pass
    def show_get_dict(self,request,instance):
        pass
    #def show_get_url(self):
    #    return self.getHtmlAddress("show")
    def show(self,request,id,url='show'):
        instance=self.get_model().objects.get(id=id)
        #self.show_fun(request,instance)
        self.show_submit(request,instance)
        dict=self.show_get_dict(request,instance) or {}
        dict['instance']=instance
        return self.show_response(request,instance,dict,url)
        #return render_to_response(self.getHtmlAddress(url), dict,context_instance=RequestContext(request))


    def index(self,request):
        return render_to_response(self.getHtmlAddress("index"), context_instance=RequestContext(request))

    def showThisPage(self,request):
        return HttpResponseRedirect(request.META['HTTP_REFERER']+"?redirect=1")
    def showUrl(self,url):
        return HttpResponseRedirect(url)
    def showUrlReverse(self,name,args=()):
        return HttpResponseRedirect(reverse(name,args=args))


    def form_save(self,request,form,dict={},change=True):
        instance = form.save(commit=False)
        add_save_prev=self.add_save_prev(request,instance,change)
        #raise Exception,add_save_prev
        if add_save_prev==True:
            instance.save()
        else:
            return add_save_prev
        if self.add_save_next(request,instance,change):
            pass
        return self.add_response_succeed(request,dict,instance)

    def add_get_form(self,request):
        pass
    def add_get_dict(self,request):
        return {}
    def add_save_prev(self,request,instance,change):
        return True
    def add_save_next(self,request,instance,change):
        return True
    def add_get_url(self,url='add'):
        return self.getHtmlAddress(url)
    def add_response_succeed(self,request,dict,instance):
        return render_to_response(self.add_get_url(),dict ,context_instance=RequestContext(request))
    def add_response(self,request,dict,instance):
        return render_to_response(self.add_get_url(),dict ,context_instance=RequestContext(request))
    def add_add_get_form(self,request,**kwargs):
        form=self.add_get_form(request)
        return form(**kwargs)
    #def add_add_post_form(self,request):
    #    return self.add_get_form(request.POST)
    #def add_update_post_form(self,request,instance):
    #    return self.add_get_form(request.POST,instance=instance)
    #def add_update_get_form(self,request,instance):
    #    return self.add_get_form(instance=instance)
    def add(self,request,id='0'):
        #raise Exception,type(id)
        dict=self.add_get_dict(request)
        form=self.add_get_form(request)
        instance=None
        if form!=None:
            #添加
            if id=='0':
              if request.method=="POST":
                  #
                  form=form(request.POST)
                  if form.is_valid():
                      return self.form_save(request,form,dict=dict,change=False)
                  else:
                      form=form
              else:
                  form=self.add_add_get_form(request)
            else:
                instance=get_object_or_404(self.get_model(),pk=int(id))
                #修改
                if (request.method=="POST"):
                    form=form(request.POST,instance=instance)
                    if form.is_valid():
                        return self.form_save(request,form,dict=dict,change=True)
                else:
                    form=form(instance=instance)
            dict["form"]=form
        return self.add_response(request,dict,instance)

    def delete_response(self,request,instance):
        return HttpWrite.infoSuccess(request,u'删除成功！')
    def delete_prev(self,request,instance):
        return True
    def delete(self,request,id=''):
        instance=get_object_or_404(self.get_model(),pk=int(id))
        prev = self.delete_prev(request,instance)
        if prev==True:
            instance.delete()
        else:
            return prev
        return self.delete_response(request,instance)
